# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc


class aas_personal_enrollment(osv.TransientModel):
    _name = 'aas.personal.enrollment'
    _columns = {
        'enrollment_lines': fields.many2many('aas.course.student', 'personal_enrollment_course_student', 'enrollment_id', 
                                                'course_student_id', string='Details', readonly=True, context={'enrollment':1}),
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_personal_enrollment, self).default_get(cr, uid, fields, context=context)
        
        course_obj = self.pool.get('aas.course.course')
        course_student_obj = self.pool.get('aas.course.student')
        self.registers_ids = course_student_obj.search(cr, uid, [('course_id.is_in_current_academic_state', '=', 1), 
                                                        ('student_id', '=', uid),
                                                        ('course_id.state', 'not in', ('draft1',))], context=context)
        self.list_id = False
         
        if self.registers_ids:
            sql = '''
                SELECT list_id
                FROM aas_course_course
                WHERE id IN (
                    SELECT course_id
                    FROM aas_course_student
                    WHERE id = %s
                )
            '''
            cr.execute(sql, (self.registers_ids[0],))
            if cr.rowcount > 0:
                self.list_id = cr.fetchone()[0]
        res.update({'enrollment_lines': self.registers_ids})
        return res
    
    def print_report(self, cr, uid, ids, context=False):
        datas = {}
        
        if not self.registers_ids:
            raise osv.except_osv(_('Thông báo!'), _('Thông tin đăng ký học phần trống, không thể tải về bản in!'))
        
        datas['model'] = 'res.users'
        datas['id'] = uid
        datas['ids'] = [uid]
        return {
            'type': 'ir.actions.report.xml', 
            'report_name': 'aas_enrollment_report', 
            'datas': datas,
            'context': {'student_id': uid, 'registers_ids': self.registers_ids}, 
        }
    
aas_personal_enrollment()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
